﻿using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台管理系统-药品模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class DrugController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IDrugServices _drugServices;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="DrugServices"></param>

        public DrugController(IDrugServices DrugServices)
        {
            _drugServices = DrugServices;
        }
        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<Drug>>> Get(int page = 1, int  intPageSize = 50)
        {
            //相当于linq里面查询里的 where(x=>x.id==id)

            Expression<Func<Drug, bool>> whereExpression = Drug => Drug.IsDeleted == false;

            return new MessageModel<PageModel<Drug>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<MessageModel<Drug>> Get(string id)
        {
            return new MessageModel<Drug>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugServices.QueryById(id)
            };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost] //FromBody 是将方法内的参数以json方式传递
        public async Task<MessageModel<string>> Post([FromBody] Drug request)
        {
            var data = new MessageModel<string>();
           
            var id = await _drugServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }
       /// <summary>
       /// 编辑
       /// </summary>
       /// <param name="request"></param>
       /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] Drug request)
        {
            var data = new MessageModel<string>();
            data.success = await _drugServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.DrugId.ObjToString();
            }

            return data;
        }

       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();//querybyid 按照id查询
            var model = await _drugServices.QueryById(id);
            model.IsDeleted = true;
            data.success = await _drugServices.Delete(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.DrugId.ObjToString();
            }

            return data;
        }
    }
}